package com.itheima.goods.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.StatusConstant;
import com.itheima.context.BaseContext;
import com.itheima.goods.dto.CategoryDTO;
import com.itheima.goods.dto.CategoryPageQueryDTO;
import com.itheima.goods.mapper.CategoryMapper;
import com.itheima.goods.pojo.Category;
import com.itheima.goods.pojo.Dish;
import com.itheima.goods.pojo.Setmeal;
import com.itheima.goods.service.CategoryService;

import com.itheima.result.PageBean;
import com.itheima.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public PageBean page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> categories= categoryMapper.pageSelect(categoryPageQueryDTO);
        return new PageBean(categories.getTotal(),categories.getResult());
    }



    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.addCategory(category);
    }
    @Override
    public void setStatus(Integer status, Long id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        categoryMapper.update(category);
    }
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    @Override
    public Result deleteById(Long id) {
        Category category=categoryMapper.selectById(id);
        //查看该分类下有没有菜品或者套餐
        if (category.getType()==1){
            List<Dish> dishes = categoryMapper.listDishByType(category.getId());
            if (dishes.size()>0){

                return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
            }
        }
        if (category.getType()==2){
            List<Setmeal> meals = categoryMapper.medallistByType(category.getId());
            if (meals.size()>0){
                return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
            }
        }
        categoryMapper.deleteById(id);
        return Result.success();
    }

    @Override
    public List<Category> select(CategoryDTO categoryDTO) {
        Integer type=categoryDTO.getType();
       List<Category> list= categoryMapper.listByType(type);
        return list;
    }
//+++++++======================================================
    /**
     * 用户端条件查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> selectByType(Long type) {
       List<Category> categories= categoryMapper.selectByType(type);
        return categories;
    }

    @Override
    public List<Category> all() {
       List<Category> categories= categoryMapper.all();
       return categories;
    }
}
